package com.fm.controller;

import com.alibaba.fastjson.JSON;
import com.fm.entity.Product;
import com.fm.entity.ProductFunction;
import com.fm.entity.ProductTopic;
import com.fm.service.ProductFunctionService;
import com.fm.service.ProductService;
import com.fm.service.ProductTopicService;
import com.fm.utils.JSONUtils;
import com.fm.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/7/30 0030.
 */
@RestController
@RequestMapping("/api/productFunction")
public class ProductFunctionController extends MBaseController<ProductFunction, ProductFunctionService>{

    @Autowired
    private ProductService productService;

    @Override
    public ProductFunction beforeList(Map t) {
        ProductFunction topic = new ProductFunction();
        topic.setProductId(Integer.parseInt((String) t.get("productId")));
        topic.setParentId(0);
        return topic;
    }

    @Override
    public void afterSave(ProductFunction productFunction, HttpServletRequest request) {
        if(productFunction.getParams()!=null&&productFunction.getParams().size()>0) {
            for(ProductFunction function : productFunction.getParams()) {
                function.setProductId(productFunction.getProductId());
                function.setParentId(productFunction.getId());
                if (function.getId()==null)
                    this.baseService.insert(function);
                else
                    this.baseService.update(function);
            }
        }
        //修改物模型
        Product product = productService.selectOneByExample(productFunction.getProductId()).get();
        String json = JSONUtils.toJSONString(productService.formatObjectModal(product.getId()));
        product.setObjectModal(json);
        productService.update(product);
    }

    @Override
    public void afterList(Page<ProductFunction> page) {
        for (ProductFunction p : page.getContent()){
            ProductFunction exam = new ProductFunction();
            exam.setParentId(p.getId());
            p.setParams(this.baseService.selectByExample(exam));
        }
    }

    @RequestMapping(value = "/getFunParamTemplate")
    @ResponseBody
    public R getFunParamTemplate(Integer id) {
        ProductFunction function = baseService.selectOneByExample(id).get();
        Map remap = new HashMap();
        if (function.getType() == 1){//属性
            remap.put(function.getIdentifier(),"");
        }else {
            ProductFunction t = new ProductFunction();
            t.setParentId(id);
            t.setType(4);
            List<ProductFunction> functionList = baseService.selectByExample(t);
            for (ProductFunction f : functionList) {
                remap.put(f.getIdentifier(),"");
            }
        }
        return R.ok().put("data", remap);
    }
}
